package p.ithorns.sample.refresh;

import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;
import java.util.Properties;

/**
 * RefreshEndpoint
 *
 * RestControllerEndpoint可以共用MVC相关的注解对端点进行“完全”控制
 * Endpoint只能使用@ReadOperation,@WriteOperation和@DeleteOperation
 *
 * @author HtL
 * @date 2024/12/10 13:00
 * @since 1.0.0
 */
@Component
// @Endpoint(id = "refresh")
@RestControllerEndpoint(id = "refresh")
public class RefreshEndpoint {

    private final ConfigChangeListener listener;

    public RefreshEndpoint(ConfigChangeListener listener) {
        this.listener = listener;
    }

    @PostMapping
    public String refresh(@RequestBody Map<String,Object> properties) {
        listener.refreshConfig(properties);
        return "Configuration refreshed.";
    }

//    @WriteOperation
//    public String refresh(String key, String value ) {
//        Properties props = new Properties();
//        props.put(key, value);
//        configRefreshService.refreshConfig(props);
//        return "Configuration refreshed.";
//    }

}